<template>
	<view>
		<view class="home_tab">
			<view class="home_tab_title">
				<view class="title_inner">
					<uni-segmented-control :current="current" :values="items.map(v=>v.title)" @clickItem="onClickItem" style-type="text"
					 active-color="#d4237a"></uni-segmented-control>
				</view>
				<view class="iconfont iconsearch"></view>
			</view>
			<scroll-view scroll-y enable-flex class="home_tab_content" @scrolltolower="handleScrolltolower">
				<view class="cate_item" v-for="item in vertical" :key="item.id">
					<image :src="item.thumb" mode="widthFix"></image>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import uniSegmentedControl from "@/components/uni-segmented-control/uni-segmented-control.vue"

	export default {
		components: {
			uniSegmentedControl
		},
		data() {
			return {
				items: [{
						title: '最新',
						order: 'new'
					},
					{
						title: '热门',
						order: 'hot'
					}
				],
				current: 1,
				params: {
					limit: 30,
					skip: 0,
					order: 'new'
				},
				id: 0,
				vertical: [],
				hasMore: true
			};
		},
		onLoad(options) {
			this.id = options.id;
			this.getList()
		},
		methods: {
			onClickItem(index) {


				if (this.current !== index.currentIndex) {
					this.current = index.currentIndex;
					uni.setNavigationBarTitle({
						title: this.items[this.current].title
					})
				} else {
					return;
				}
				this.params.order = this.items[index.currentIndex].order;
				this.params.skip = 0;
				this.vertical = [];
				this.getList()
			},
			getList() {
				this.request({
					url: `http://157.122.54.189:9088/image/v1/vertical/category/${this.id}/vertical`,
					data: this.params
				}).then(result => {
					if (result.res.vertical.length === 0) {
						this.hasMore = false;
						uni.showToast({
							title: '没有 更多数据了',
							icon: 'none'
						});
					}


					this.vertical = [...this.vertical, ...result.res.vertical];
				})
			},
			handleScrolltolower() {
				if (this.hasMore) {
					this.params.skip += this.params.limit;
					this.getList()
				} else {
					uni.showToast({
						title: '没有等多数据',
						icon: 'none'
					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.home_tab {
		.home_tab_title {
			position: relative;

			.title_inner {
				width: 60%;
				margin: 0 auto;
			}

			.iconsearch {
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				right: 5%;
			}
		}

		.home_tab_content {
			display: flex;
			flex-wrap: wrap;
			height: calc(100vh - 36px);

			.cate_item {
				width: 33.33%;
				border: solid 2rpx #FFFFFF;
			}

			image {}
		}
	}
</style>
